package com.Xht.www.dao;

import com.Xht.www.po.RoomBookingSchedulePO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoomBookingScheduleDAO extends BaseDAO {

    // 插入房间预订时间表信息
    public void insertRoomBookingSchedule(RoomBookingSchedulePO schedule) {
        if (isInsertInputInvalid(schedule)) {
            System.err.println("插入房间预订时间表信息时，传入的 RoomBookingSchedulePO 对象或其属性不符合要求");
            return;
        }
        String sql = "INSERT INTO room_booking_schedule (order_id, room_type_name, bed_type, booking_date, is_available) VALUES (?,?,?,?,?)";
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, schedule.getOrderId());
            pstmt.setString(2, schedule.getRoomTypeName());
            pstmt.setString(3, schedule.getBedType());
            pstmt.setDate(4, schedule.getBookingDate());
            pstmt.setBoolean(5, schedule.getIsAvailable());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 验证插入操作的输入是否无效
    private boolean isInsertInputInvalid(RoomBookingSchedulePO schedule) {
        return schedule == null ||
                schedule.getOrderId() <= 0 ||
                schedule.getRoomTypeName() == null || schedule.getRoomTypeName().trim().isEmpty() ||
                schedule.getBedType() == null || schedule.getBedType().trim().isEmpty() ||
                schedule.getBookingDate() == null ||
                schedule.getIsAvailable() == null;
    }

    // 根据时间表 ID 删除房间预订时间表信息
    public void deleteRoomBookingScheduleById(int scheduleId) {
        if (scheduleId <= 0) {
            System.err.println("删除房间预订时间表信息时，传入的时间表 ID 必须大于 0");
            return;
        }
        String sql = "DELETE FROM room_booking_schedule WHERE schedule_id = ?";
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, scheduleId);
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 根据时间表 ID 查询房间预订时间表信息
    public RoomBookingSchedulePO getRoomBookingScheduleById(int scheduleId) {
        if (scheduleId <= 0) {
            System.err.println("查询房间预订时间表信息时，传入的时间表 ID 必须大于 0");
            return null;
        }
        String sql = "SELECT schedule_id, order_id, room_type_name, bed_type, booking_date, is_available FROM room_booking_schedule WHERE schedule_id = ?";
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, scheduleId);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    RoomBookingSchedulePO schedule = new RoomBookingSchedulePO();
                    schedule.setScheduleId(rs.getInt("schedule_id"));
                    schedule.setOrderId(rs.getInt("order_id"));
                    schedule.setRoomTypeName(rs.getString("room_type_name"));
                    schedule.setBedType(rs.getString("bed_type"));
                    schedule.setBookingDate(rs.getDate("booking_date"));
                    schedule.setIsAvailable(rs.getBoolean("is_available"));
                    return schedule;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    // 查询所有房间预订时间表信息
    public List<RoomBookingSchedulePO> getAllRoomBookingSchedules() {
        List<RoomBookingSchedulePO> schedules = new ArrayList<>();
        String sql = "SELECT schedule_id, order_id, room_type_name, bed_type, booking_date, is_available FROM room_booking_schedule";
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {
            while (rs.next()) {
                RoomBookingSchedulePO schedule = new RoomBookingSchedulePO();
                schedule.setScheduleId(rs.getInt("schedule_id"));
                schedule.setOrderId(rs.getInt("order_id"));
                schedule.setRoomTypeName(rs.getString("room_type_name"));
                schedule.setBedType(rs.getString("bed_type"));
                schedule.setBookingDate(rs.getDate("booking_date"));
                schedule.setIsAvailable(rs.getBoolean("is_available"));
                schedules.add(schedule);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return schedules;
    }

    // 更新房间预订时间表信息
    public void updateRoomBookingSchedule(RoomBookingSchedulePO schedule) {
        if (isUpdateInputInvalid(schedule)) {
            System.err.println("更新房间预订时间表信息时，传入的 RoomBookingSchedulePO 对象或其属性不符合要求");
            return;
        }
        String sql = "UPDATE room_booking_schedule SET order_id = ?, room_type_name = ?, bed_type = ?, booking_date = ?, is_available = ? WHERE schedule_id = ?";
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setInt(1, schedule.getOrderId());
            pstmt.setString(2, schedule.getRoomTypeName());
            pstmt.setString(3, schedule.getBedType());
            pstmt.setDate(4, schedule.getBookingDate());
            pstmt.setBoolean(5, schedule.getIsAvailable());
            pstmt.setInt(6, schedule.getScheduleId());
            pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 验证更新操作的输入是否无效
    private boolean isUpdateInputInvalid(RoomBookingSchedulePO schedule) {
        return schedule == null ||
                schedule.getScheduleId() == null || schedule.getScheduleId() <= 0 ||
                schedule.getOrderId() <= 0 ||
                schedule.getRoomTypeName() == null || schedule.getRoomTypeName().trim().isEmpty() ||
                schedule.getBedType() == null || schedule.getBedType().trim().isEmpty() ||
                schedule.getBookingDate() == null ||
                schedule.getIsAvailable() == null;
    }
}